package com.atguigu.dga.assess.assess.cal;

import com.atguigu.dga.assess.assess.AssessorTemplate;
import com.atguigu.dga.assess.bean.AssessParam;
import com.atguigu.dga.assess.bean.GovernanceAssessDetail;
import com.atguigu.dga.config.MetaConstant;
import com.atguigu.dga.ds.bean.TDsTaskInstance;
import com.atguigu.dga.ds.service.TDsTaskInstanceService;
import com.atguigu.dga.meta.bean.TableMetaInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("TASK_FAILED")
public class CheckTaskFailure extends AssessorTemplate {
    @Autowired
    private TDsTaskInstanceService instanceService;
    /*
    检查DS 有报错 则给0分，其余给10分
            在ds的t_ds_task_instance表中
                一个task_instance，如果state是6，就是失败
                                 如果state是7，就是成功
             如何判断t_ds_task_instance表中的哪一行是要考评的表的运行信息？
                在调度时，强制Task的名字必须以 库名.表名的格式进行命名
            -------------------------
            DS是一天一调度
                    考评日期和DS的调度日期是同一天
                    20 a 2023-02-02  xxx
                    40 a 2023-02-03 xxx
     */
    @Override
    protected void assess(AssessParam param, GovernanceAssessDetail detail) throws Exception {
        //获取当前表的名字
        TableMetaInfo tableMetaInfo = param.getTableMetaInfo();
        String name = tableMetaInfo.getSchemaName() + "." + tableMetaInfo.getTableName();
        //查询t_ds_task_instance表中，当前表计算的任务是否是6
        QueryWrapper<TDsTaskInstance> queryWrapper = new QueryWrapper<TDsTaskInstance>()
                .eq("name", name)
                .eq("date(start_time)", param.getAssessDate())
                .eq("state", MetaConstant.TASK_STATE_FAILED);
        TDsTaskInstance result = instanceService.getOne(queryWrapper);

        //查到，说明当前表在调度执行时报错
        if (result != null){
            assessZeroScore(detail,"执行时报错，参考运行实例id:" + result.getId(),param,false);
        }
    }
}
